package com.webserver.core;

import com.sun.security.ntlm.Client;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.net.Socket;
import java.net.URISyntaxException;


public class ClientHandler implements Runnable{
    private Socket socket;
    public ClientHandler(Socket socket){
        this.socket=socket;
    }

    public void run() {

        try {
            HttpServletRequest request = new HttpServletRequest(socket);
            HttpServletResponse response = new HttpServletResponse(socket);
            String path=request.getUri();

            File rootDir=new File(
                    ClientHandler.class.getClassLoader()
                    .getResource(".").toURI()
            );

            File staticDir= new File(rootDir,"static");

            File file=new File(staticDir,path);
            System.out.println("资源是否存在:"+file.exists());

            if(file.isFile()){
                response.setContentFile(file);
            }else {
                file = new File(staticDir, "/root/404.html");

                response.setStatusCode(404);
                response.setStatusReason("NotFound");
                response.setContentFile(file);
            }
            response.response();






        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }



}
